GO_ARCH:= $(shell go env GOARCH)
gitTime:= $(shell git log -1 --format="%cd" --date=format:'%Y-%m-%d %H:%M:%S')
buildTime=$(shell date +'%Y-%m-%d %H:%M:%S')
gitHash=$(shell git rev-parse HEAD | cut -c1-19)
gitTag=$(shell git tag --list --sort=version:refname 'v*' | tail -1)
gitCount=$(shell git rev-list --all --count)
# uiTime=$(shell cat resource/public/dist/env | grep UI_BUILD_TIME | cut -d ':' -f 2-)
# 清理可能存在的空格
buildStr=${gitTime}.${gitHash}.${gitTag}.${gitCount}
# 获取当前用户的 UID 和 GID
UID=$(shell id -u)
GID=$(shell id -g)
fileTime=$(shell date +%Y%m%d%H%M)
programName=server
buildVer=server
#or:v1
ImageFullName=leehom/detect:centos7.go1.21.7

.PHONY: init
init:
	go install github.com/google/wire/cmd/wire@latest
	go install github.com/golang/mock/mockgen@latest
	go install github.com/swaggo/swag/cmd/swag@latest

.PHONY: bootstrap
bootstrap:
	cd ./deploy/docker-compose && docker compose up -d && cd ../../
	go run ./cmd/migration
	nunu run ./cmd/server

.PHONY: mock
mock:
	mockgen -source=internal/service/user.go -destination test/mocks/service/user.go
	mockgen -source=internal/repository/user.go -destination test/mocks/repository/user.go
	mockgen -source=internal/repository/repository.go -destination test/mocks/repository/repository.go

.PHONY: test
test:
	go test -coverpkg=./internal/handler,./internal/service,./internal/repository -coverprofile=./coverage.out ./test/server/...
	go tool cover -html=./coverage.out -o coverage.html

.PHONY: build
build:buildprepare buildgo

.PHONY: buildprepare
buildprepare:
	go mod tidy
	go mod vendor

.PHONY: buildgo
buildgo:
	docker run --rm -it -e CGO_ENABLED=0 -u ${UID}:${GID} -v $(shell pwd)/:/app -w /app ${ImageFullName} go build -mod vendor \
	-o ./bin/0.0.${gitCount}/linux_amd64/${programName} \
	-ldflags "\
	-X \"gitee.com/clh21/proj-layout-advanced/pkg/version.BuildTime=${buildTime}\" \
	-X \"gitee.com/clh21/proj-layout-advanced/pkg/version.GitTime=${gitTime}\" \
	-X \"gitee.com/clh21/proj-layout-advanced/pkg/version.GitHash=${gitHash}\" \
	-X \"gitee.com/clh21/proj-layout-advanced/pkg/version.GitCount=${gitCount}\" \
	" \
	./cmd/${buildVer}/

.PHONY: docker
docker:
	docker build -f deploy/build/Dockerfile --build-arg APP_RELATIVE_PATH=./cmd/task -t 1.1.1.1:5000/demo-task:v1 .
	docker run --rm -i 1.1.1.1:5000/demo-task:v1

# https://github.com/swaggo/swag/blob/master/README.md#declarative-comments-format
.PHONY: swag
swag:
	swag init  -g cmd/server/main.go -o ./docs --parseDependency
